package com.anysoft.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by lty on 2017/12/26.
 */

public class StreamHelper {

    /**
     * 将InputStream转换为string
     *
     * @param is
     * @return
     */
    public static String parseInputStream2String(InputStream is) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        if (is != null) {
            try {
                int len;
                while ((len = is.read()) != -1) {
                    bos.write(len);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bos.toString() == null ? "" : bos.toString();
    }
    
    /**
     * 将一个字符串转化为输入流
     */
    public static InputStream parseString2InputStream(String string) {
        if (string != null) {
            try {
                ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(string.getBytes());
                return tInputStringStream;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }
}
